import {
	exec,
} from 'node:child_process';
import {defineConfig} from '../index.js';

export default defineConfig(async (
	{
		returnCheck,
		baseDir,
		packageJson,
		args,
		command,
		utils:{
			extend,
			copyReplace,
			copyCover,
			copyCustom,
			dateFormat,
		},
	}
) => {
	const branch = await new Promise<string>((resolve) => {
		exec('git rev-parse --abbrev-ref HEAD', (error, stdout) => {
			resolve(error ? '' : stdout.trim());
		});
	});
	const gitUserName = await new Promise<string>((resolve) => {
		exec('git config user.name', (error, stdout) => {
			resolve(error ? '' : stdout.trim());
		});
	});
	const lastCommit= await new Promise<string>((resolve) => {
		exec('git log -1 --pretty=format:"[%ad] %an [%h] %s" --date=format:"%Y-%m-%d %H:%M"', (error, stdout) => {
			resolve(error ? '' : stdout.trim());
		});
	});
	return returnCheck({
		extendConfigs:[
		],
		envPath:'env',
		extraEnv:{
			buildTime:dateFormat(new Date(),'yyyy-MM-dd HH:mm:ss.SSS ZZ'),
			command,
			packageName:packageJson.name,
			name:packageJson.description,
			version:packageJson.version,
			branch,
			gitUserName,
			lastCommit,
			vueVersion:require('vue/package.json').version,
			webpackVersion:require('webpack/package.json').version,
		},
	});
},async (
	{
		extendConfigs,
		envPath,
		extraEnv,
		returnCheck,
		baseDir,
		packageJson,
		args,
		command,
		utils:{
			extend,
			copyReplace,
			copyCover,
			copyCustom,
			dateFormat,
		},
	}
) => {
	return returnCheck({
		unique:packageJson.name,
		debug:false,
		devServer:{
			hot:command==='serve',
			client:{
				logging:'warn',
				overlay:{
					errors:true,
					warnings:false,
					runtimeErrors:false,
				},
			},
		},
		emitEntryInjectJs:{
		},
		pages:{
			index:'src/main.ts',
		},
		htmlTemplate:{
			favicon:'template/favicon.ico',
			minify:undefined,
			templateParameters:{
			},
			inject:false,
		},
		publicPath:'auto',
		outputDir:'dist',
		assetsDir:'assets',
		alias:{
		},
		buildFile:{
			emit:command!=='serve',
			name:'build-info.txt',
			content:[
				`构建时间: ${extraEnv.buildTime}`,
				`构建命令: ${extraEnv.command}`,
				`项目名称: ${extraEnv.packageName}`,
				`项目描述: ${extraEnv.name}`,
				`构建版本: ${extraEnv.version}`,
				`构建分支: ${extraEnv.branch}`,
				`构建用户: ${extraEnv.gitUserName}`,
				`最新提交: ${extraEnv.lastCommit}`,
				`vue版本: ${extraEnv.vueVersion}`,
				`webpack版本: ${extraEnv.webpackVersion}`,
			],
		},
		emitFiles:{
		},
		definePlugin:{
			__VUE_OPTIONS_API__:'true',
			__VUE_PROD_DEVTOOLS__:'false',
			__VUE_PROD_HYDRATION_MISMATCH_DETAILS__:'false',
		},
		vConsole:{
			active:false,
			vue:true,
		},
		buildConfig:{
			buildConsole:'normal',
			minimize:'swc',
			sourceMap:true,
			cssSourceMap:false,
			sourceLog:true,
		},
		libConfig:{
			html:{
				template:'template/index.html',
				templateContent:false,
				filename:'index.html',
				title:'',
				favicon:undefined,
				minify:true,
				templateParameters:undefined,
				inject:undefined,
				publicPath:undefined,
				base:undefined,
				meta:undefined,
				scriptLoading:'blocking',
			},
			buildConsole:'normal',
			name:packageJson.name,
			entry:'src/lib.ts',
			minimize:'swc',
			sourceMap:true,
			cssSourceMap:false,
			sourceLog:true,
			externals:{
				vue:{
					commonjs:'vue',
					commonjs2:'vue',
					amd:'vue',
					root:'Vue',
				},
				'vue-router':{
					commonjs:'vue-router',
					commonjs2:'vue-router',
					amd:'vue-router',
					root:'VueRouter',
				},
			},
			export:'default',
			polyfillAll:true,
			asyncChunks:false,
		},
		//region 压缩配置(一般不配置)
		terserOptions:{
			default:{
				ecma:5,
				safari10:true,
				compress:{
					ecma:5,
					keep_infinity:true,
					passes:2,
					reduce_funcs:false,
				},
				mangle:{
					safari10:true,
				},
				format:{
					ecma:5,
					comments:false,
					safari10:true,
				},
			},
			swc:{
				compress:{
					ecma:5,
					keep_infinity:true,
					passes:0,
					reduce_funcs:false,
				},
				mangle:{
					safari10:true,
				},
				format:{
					ecma:5,
					comments:false,
					safari10:true,
				},
			},
		},
		//endregion
		copy:{
			dir:'static',
			to:undefined,
			toType:undefined,
			minimize:true,
			filter:undefined,
			transformer:undefined,
		},
		babel:{
			presetOption:{
				browserslistEnv:undefined,
				debug:false,
				useBuiltIns:'usage',
			},
			plugins:[
			],
			polyfills:[
				'stable/object',
				'stable/array',
				'stable/set',
				'stable/map',
				'stable/promise',
				'stable/url',
			],
			extraPolyfills:[
				'intersection-observer',
			],
			transpileDependencies:[
				'@vue/devtools-api',
				'vue-demi',
			],
			extraEntry:[
			],
		},
		typescript:{
			active:true,
			configFile:undefined,
		},
		eslint:{
			active:true,
			failOnError:command!=='serve',
			configType:'flat',
			eslintPath:'eslint/use-at-your-own-risk',
			extensions:[
				'js',
				'ts',
				'vue',
			],
		},
		markdown:{
			components:{
			},
			elementClassName:{
				table:'apiTable',
			},
			cssModules:[
				'powerful-cli/styles/markdown.module.scss',
			],
		},
		css:{
		},
		cacheGroups:{
			base:{
				priority:20,
				test:/[\\/]node_modules[\\/](?:core-js|@babel[\\/]runtime|@vue|vue(?:x|-router|-loader)?|pinia)[\\/]/,
				name:'base',
				reuseExistingChunk:true,
				enforce:true
			},
			lib:{
				priority:10,
				test:/[\\/]node_modules[\\/]/,
				name:'lib',
				reuseExistingChunk:true,
				enforce:true
			},
		},
		splitChunks:{
			minSize:500 * 1024,
			enforceSizeThreshold:5 * 1024 * 1024,
			maxInitialRequests:1,
			maxAsyncRequests:1,
		},
		report:{
			emit:true,
			type:'html',
			filename:'report',
			statsOptions:null,
		},
		fileManager:undefined,
		webpackConfig:{
		},
	});
});